home *** CD-ROM | disk | FTP | other *** search
- /* Regression Statistics */
-
- options results
- if ~show('P','TCALC') then do
- address command 'run turbocalc:turbocalc'
- address command 'waitforport TCALC'
- loadflag=1
- end
- address 'TCALC'
- 'DEFPUBSCREEN()'
- /* Add-in Rexx Math Library needed for some routines */
- signal on syntax
- if ~show('l','rexxmathlib.library') then
- call addlib('rexxmathlib.library',0,-30)
- if ~show('l','rexxreqtools.library') then
- call addlib('rexxreqtools.library',0,-30)
- if ~show('l','rexxsupport.library') then
- call addlib('rexxsupport.library',0,-30)
- /* add to library list */
- signal off syntax
-
- /* Start Main Routine */
- if loadflag=1 then 'Load()'
- 'ActivateWindow()'
- range=rtgetstring(,"Enter Cell Range for Input","Input Request") /*,,'rt_pubscrname="TCALC"')*/
- colon=pos(":",range)
- if colon=0 then do
- 'Message "Please select a range before executing this script"'
- 'DEFPUBSCREEN("Workbench")'
- exit
- end
-
- /* Find cell references and cell, column numbers */
- start_cell=substr(range,1,colon-1)
- end_cell=substr(range,colon+1)
- start_row=cellrow(start_cell)
- end_row=cellrow(end_cell)
- start_col=cellcol(start_cell)
- end_col=cellcol(end_cell)
- NRows=end_row-start_row+1
- NCols=end_col-start_col+1
- if NCols>2 then do
- 'Message "Only Two Columns allowed!"'
- 'DEFPUBSCREEN("Workbench")'
- exit
- end
-
- /* Ensure we have X and Y in the right columns */
- in=2 /* Independent variable x in second column */
- de=1 /* Dependent variable y in first column */
- an=rtgetstring(,"Assume Dependent Variable is First Column?: Y or N","Input Request") /*,,'rt_pubscrname="TCALC"')*/
- if an="" | rtresult=0 then do
- 'Message "Aborting!"'
- 'DEFPUBSCREEN("Workbench")'
- exit
- end
- an=left(an,1)
- an=Upper(an)
- if an="N" then do
- in=1
- de=2
- end
-
- /* Get cell reference for output range */
- out_cell=rtgetstring(,"Enter Cell Reference for Output","Input Request") /*,,'rt_pubscrname="TCalc"')*/
- if out_cell="" then do
- 'DEFPUBSCREEN("Workbench")'
- exit
- end
- if length(out_cell)<2 | datatype(left(out_cell,1),'n') then do
- 'Message "Invalid cell reference"'
- 'DEFPUBSCREEN("Workbench")'
- exit
- end
- /* Suppress Screen Redraw to Speed Things Up */
- 'Refresh 0'
-
- /* Open a small output window on tcalc screen*/
- fo=0
- CR='0a'x
- DisplayMsg="Calculating...Please Wait."||CR||"User input is disabled during calculation."||CR
- if open(6Info, 'con:100/0/450/80/Progress/SCREEN TCALC', w) then do
- call writeln(6Info, DisplayMsg)
- fo=1
- end
- else do
- 'Message "TCALC Screen not available for Progress messages"'
- end
- CALL DELAY(150)
-
- /* Get cell references for top cell in each column */
- 'SelectCell' start_cell
- do col=start_col to end_col
- 'GetCursorPos'
- top_cell.col=result
- 'Column 1'
- end
- if fo then call writech(6Info,"Progress...10 ")
-
- /* Get labels for later use on output */
- 'SelectCell' start_cell
- 'GetValue'
- testlabel=result
- testlabel=strip(testlabel)
- if datatype(testlabel,'n')=1 then do
- labelflag=0
- do x=1 to NCols
- title.x="Column "||x
- end
- end
- else do
- labelflag=1
- NRows=NRows-1
- do x=1 to NCols
- 'GetValue'
- str=result
- title.x=translate(strip(str),"_"," ")
- 'Column 1'
- end
- end
- if fo then call writech(6Info,"20 ")
-
- /* Get data from cell range */
- col=start_col
- lav=0
- tot=0
- count.=0
- total.=0
- do x=1 to NCols
- 'SelectCell' top_cell.col
- if labelflag=1 then 'CursorDown 1'
- do y=1 to NRows
- 'GetValue'
- valtest=result
- if datatype(valtest)='NUM' then do
- 'GetValue'
- val=result
- data.x.y=val
- tot=tot+val
- total.x=tot
- count.x=1+count.x
- end
- 'CursorDown 1'
- end
- col=col+1
- tot=0
- lav=0
- val=0
- end
- if count.1 ~= count.2 then do
- 'Message "There must be equal numbers of values in both columns"'
- 'DEFPUBSCREEN("Workbench")'
- exit
- end
- if fo then call writech(6Info,"30 ")
-
- /* Calculate Means */
- mean.=0
- do x=1 to NCols
- mean.x=total.x/count.x
- end
-
- /* Calculate Standard deviation and Variance */
- dat=0
- meenx=0
- tot.=0 /* Array holding sums of columns */
- totsq.=0 /* Array holding square of column sums */
- XY=0 /* Var holding X times Y */
- diff.=0 /* Array holding difference between value and column mean */
- sum.=0 /* Array holding sum of value minus mean of value squared */
- sd.=0 /* Standard deviation array */
- var.=0 /* Variance array */
- do x=1 to NCols
- sum.x=0
- meenx=mean.x
- do y =1 to count.x
- dat=data.x.y
- tot.x=dat+tot.x
- totsq.x=(totsq.x)+dat**2
- diff.x.y=dat-meenx
- sum.x=(dat-meenx)**2+(sum.x)
- end
- N=(count.x)-1
- var.x=(sum.x)/N
- sd.x=sqrt(var.x)
- end
- Do y=1 to NRows
- XY=XY+((data.1.y)*(data.2.y))
- end
- say "XY="XY
- if fo then call writech(6Info,"40 ")
- N=count.1
- summulti=0 /* Sum of product of diffs */
- diffMult.=0 /* Array holding values of diffx times diffy */
- do y=1 to N
- diffMult.y=(diff.de.y)*(diff.in.y)
- summulti=summulti+(diffMult.y)
- end
-
- /* Calculate a & b and Predicted Y */
- b=0
- a=0
- sumdifpa=0
- Pr.=0 /* Array holding values of predicted values */
- difpa.=0 /* Array holding differences between observed and predicted values */
- b=summulti/(sum.in)
- a=(mean.de)-b*(mean.in)
- do y=1 to N
- Pr.y=a+b*(data.in.y)
- difpa.y=(data.de.y)-(Pr.y)
- sumdifpa=sumdifpa+(difpa.y)**2
- end
- if fo then call writech(6Info,"50 ")
-
- /* Calculate standard Deviation */
- stdev=0
- t=0
- stdev=sqrt((sumdifpa/((N-2)*(sum.in))))
- t=b/stdev
- df=N-2 /* Degrees of Freedom */
-
- /* Calculate Pearson r */
- rs=0
- pr=0
- rs=((summulti)**2)/((sum.in)*(sum.de))
- pr=sqrt(rs)
-
- /* Calculate ANOVA */
- SSRegTop=(XY-(((tot.1)*(tot.2))/NRows))**2
- SSRegBot=(totsq.1)-(((tot.1)**2)/NRows)
- SSReg=SSRegTop/SSRegBot
- SSTot=(totsq.2)-((tot.2)**2/NRows)
- SSRes=SSTot-SSReg
- MeanSqReg=SSReg
- MeanSqRes=SSRes/df
- FR=MeanSqReg/MeanSqRes
- DFB=NCols-1
- DFW=NRows-NCols
- if fo then call writech(6Info,"60 ")
-
- /* Calculate Probability */
-
- AC=.0001
- EC=.005
- EC2=EC+EC
- PF=PROBABILITY(FR,DFB,DFW)
- PF=1-PF
-
- if fo then call writech(6Info,"70 ")
- /* P=.95 OR .99 */
- FCRIT1=FCRITICAL(.95,DFB,DFW)
- FCRIT2=FCRITICAL(.99,DFB,DFW)
-
- /* Calculate T distribution function */
-
- P=PROBT(t,df)
- if t>=0 then P=1-P
- PT=P*2
- /* Calculate T Critical */
- if fo then call writech(6Info,"80 ")
- TCRIT1=TCRITICAL(.95,df)
- TCRIT2=TCRITICAL(.99,df)
- TCRIT3=TCRITICAL(.975,df)
- if fo then call writech(6Info,"90 ")
- TCRIT4=TCRITICAL(.995,df)
- if fo then do
- call writeln(6Info,"100")
- call writeln(6Info,"Writing output to window...")
- end
- /* Output */
- PV=0
- 'SelectCell' out_cell
- 'ColumnWidth 10'
- 'Put "Least Squares Regression"'
- 'CursorDown 1'
- 'Put "Predicted Values"'
- 'CursorDown 1'
- do y= 1 to N
- 'Put' format(Pr.y,,4)
- 'CursorDown 1'
- end
- 'SelectCell' out_cell
- 'CursorDown 1'
- 'Column 2'
- 'ColumnWidth 25'
- 'Put "Regression Statistics:"'
- 'CursorDown 1'
- 'Put "N:"'
- 'CursorDown 1'
- 'Put "Pearson r:"'
- 'CursorDown 1'
- 'Put "r sq.:"'
- 'CursorDown 1'
- 'Put "Std. Error of Est.:"'
- 'CursorDown 1'
- 'Put "Intercept (a):"'
- 'CursorDown 1'
- 'Put "Slope (b):"'
- 'CursorDown 2'
- 'Put "T test:"'
- 'CursorDown 1'
- 'Put "Std. Error of Reg. Coef.:"'
- 'CursorDown 1'
- 'Put "t:"'
- 'CursorDown 1'
- 'Put "d.f.:"'
- 'CursorDown 1'
- 'Put "P(T<=t) one-tail:"'
- 'CursorDown 1'
- 'Put "T-Critical (95%):"'
- 'CursorDown 1'
- 'Put "T-Critical (99%):"'
- 'CursorDown 1'
- 'Put "P(T<=t) two-tail:"'
- 'CursorDown 1'
- 'Put "T-Critical (95%):"'
- 'CursorDown 1'
- 'Put "T-Critical (99%):"'
- 'SelectCell' out_cell
- 'CursorDown 1'
- 'Column 3'
- 'CursorDown 1'
- 'Put' NRows
- 'CursorDown 1'
- 'Put' format(pr,,4)
- 'CursorDown 1'
- 'Put' format(rs,,4)
- 'CursorDown 1'
- 'Put' format(sumdifpa,,4)
- 'CursorDown 1'
- 'Put' format(a,,4)
- 'CursorDown 1'
- 'Put' format(b,,4)
- 'CursorDown 3'
- 'Put' format(stdev,,4)
- 'CursorDown 1'
- 'Put' format(t,,4)
- 'CursorDown 1'
- 'Put' df
- 'CursorDown 1'
- 'Put' format(P,,6)
- 'CursorDown 1'
- 'Put' format(TCRIT1,,4)
- 'CursorDown 1'
- 'Put' format(TCRIT2,,4)
- 'CursorDown 1'
- 'Put' format(PT,,6)
- 'CursorDown 1'
- 'Put' format(TCRIT3,,4)
- 'CursorDown 1'
- 'Put' format(TCRIT4,,4)
- 'CursorDown 2'
- 'Column -1'
- 'Put "ANOVA:"'
- 'CursorDown 1'
- 'GetCursorPos'
- an_cell=result
- 'Put "Source of"'
- 'CursorDown 1'
- 'Put "Variation"'
- 'CursorDown 1'
- 'Put "Regression:"'
- 'CursorDown 1'
- 'Put "Residual:"'
- 'CursorDown 1'
- 'Put "Total:"'
- 'CursorDown 2'
- 'Put "P(F Sample <=f) One-tail:"'
- 'CursorDown 1'
- 'Put "F Critical (95%):"'
- 'CursorDown 1'
- 'Put "F Critical (99%):"'
- 'SelectCell' an_cell
- 'Column 1'
- 'Put "Sum of"'
- 'CursorDown 1'
- 'Put "Squares"'
- 'CursorDown 1'
- 'Put' format(SSReg,,4)
- 'CursorDown 1'
- 'Put' format(SSRes,,4)
- 'CursorDown 1'
- 'Put' format(SSTot,,4)
- 'CursorDown 2'
- 'Put' format(PF,,6)
- 'CursorDown 1'
- 'Put' format(FCRIT1,,4)
- 'CursorDown 1'
- 'Put' format(FCRIT2,,4)
- 'SelectCell' an_cell
- 'Column 2'
- 'Put "Mean"'
- 'CursorDown 1'
- 'Put "Squares"'
- 'CursorDown 1'
- 'Put' format(MeanSqReg,,4)
- 'CursorDown 1'
- 'Put' format(MeanSqRes,,4)
- 'SelectCell' an_cell
- 'Column 3'
- 'Put "Degrees of"'
- 'CursorDown 1'
- 'Put "Freedom"'
- 'CursorDown 1'
- 'Put' 1
- 'CursorDown 1'
- 'Put' df
- 'CursorDown 1'
- 'Put' df+1
- 'SelectCell' an_cell
- 'Column 4'
- 'Put "F Ratio"'
- 'CursorDown 2'
- 'Put' format(FR,,4)
- 'Refresh 1'
- 'Refresh 2'
- /*'Message' "Finished"*/
- /*indicate the main script is finished*/
- DisplayMsg="Cleaning up ...."||CR||"Exiting"
- result=writeln(6Info, DisplayMsg)
- if result~=0 then do
- /*Wait 3 seconds*/
- CALL DELAY(150)
- /* close window*/
- result=close(6Info)
- end
- 'DEFPUBSCREEN("Workbench")'
- exit
-
- /* Procedures */
-
- cellrow: procedure
- do
- parse arg cell
- do charpos=2 to length(cell)
- if datatype(substr(cell,charpos,1),n) then return substr(cell,charpos)
- end
- return 0
- end
- Return
-
- cellcol: procedure
- do
- parse arg cell
- labels="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- cell=upper(cell)
- len=length(cell)
- val=0
- do charpos=1 to len
- if datatype(substr(cell,charpos,1),n) then
- do cell=reverse(substr(cell,1,charpos-1))
- do x=1 to length(cell)
- val=(26**(x-1))*pos(substr(cell,x,1),labels)+val
- end
- return val
- end
- end
- return 0
- end
- Return
-
- syntax:
- if arg(1)='FAIL' then do
- 'Message "Library is unavailable."'
- 'DEFPUBSCREEN("Workbench")'
- exit
- end
- 'DEFPUBSCREEN("Workbench")'
- exit
-
- Format: procedure
-
- arg number, before, after
- CallLine = SIGL
- if ~datatype(CallLine, 'N') then CallLine = '??'
-
- /* Make sure we have a number as first (required) argument */
- if ~datatype(number, 'N') then do
- if number = '' then
- fc = 17 /* Wrong number of arguments */
- else
- fc = 47 /* Arithmetic conversion error */
- signal FormatSyntaxError
- end
- num = number + 0
- if before = '' & after = '' then
- return num
- else do
- parse var num integer '.' fraction
- if before = '' then before = length(integer)
- if after = '' then after = length(fraction)
- if ~datatype(before, N) | ~datatype(after, N) then
- do fc = 18
- signal FormatSyntaxError
- end
- if before < length(integer) then do
- fc = 18
- signal FormatSyntaxError
- end
- if after ~= length(fraction) then do
- fraction = trunc(('.'fraction'0') + ('.'copies('0', after)'5'), after)
- if integer<1&integer>-1 then integer=integer
- else integer = integer + (fraction % 1)
- fraction = substr(fraction, 3)
- end
- if fraction >= 0 then
- return right(integer, before)'.'fraction
- else
- return right(integer, before)
- end
-
- FormatSyntaxError:
- if show('F', STDERR) then
- call writeln(STDERR, '+++ Error' fc 'in line' CallLine':' errortext(fc))
- else
- mess='+++ Error' fc 'in line' CallLine':' errortext(fc)
- 'Message' mess
- parse source Func .
- if Func = 'FUNCTION' then do
- 'DEFPUBSCREEN("Workbench")'
- exit "Err"
- end
- else do
- 'DEFPUBSCREEN("Workbench")'
- exit 10
- end
-
- PROBT: PROCEDURE
-
- PARSE ARG TA,K1
- A=.36338023
- W=ATAN(TA/SQRT(K1))
- S=SIN(W)
- C=COS(W)
- L=K1-2*INT(K1/2)
- IF L=0 THEN DO
- T1=S
- IF K1=2 THEN DO
- PO=.5*(1+T1)
- RETURN PO
- END
- T2=S
- J1=-1
- J2=0
- K2=(K1-2)/2
- END
- ELSE DO
- T1=W
- IF K1=1 THEN DO
- T1=T1*(1-A*L)
- PO=.5*(1+T1)
- RETURN PO
- END
- T2=S*C
- T1=T1+T2
- IF K1=3 THEN DO
- T1=T1*(1-A*L)
- PO=.5*(1+T1)
- RETURN PO
- END
- J1=0
- J2=1
- K2=(K1-3)/2
- END
- DO I=1 TO K2
- J1=J1+2
- J2=J2+2
- T2=T2*C*C*J1/J2
- T1=T1+T2
- END
- T1=T1*(1-A*L)
- PO=.5*(1+T1)
- RETURN PO
-
- TCRITICAL: PROCEDURE
-
- PARSE ARG PO,K1
- AO=.0001
- E=.005
- E2=E+E
- A=2*PO-1
- IF K1=1 THEN DO
- TO=TAN(1.57079633*A)
- RETURN TO
- END
- IF K1=2 THEN DO
- SN=SIGN(2/(1-A*A))
- IF SN=-1 THEN TO=-1*(A*SQRT(ABS(2/(1-A*A))))
- ELSE TO=A*SQRT(2/(1-A*A))
- RETURN TO
- END
- P1=PO
- Z=NORMALPP(PO)
- W=Z*(1+2/(1+8*K1))
- T3=K1*(EXP(W*W/K1)-1)
- SELECT
- WHEN Z<0 THEN TT=-1
- WHEN Z=0 THEN TT=0
- OTHERWISE TT=1
- END
- SN=SIGN(T3)
- IF SN=-1 THEN T3=-1*(TT*SQRT(ABS(T3)))
- ELSE T3=TT*SQRT(T3)
- /* LABELA:
- TO=T3
- PO=PROBT(TO,K1)
- F1=PO-P1
- TO=T3+E
- PO=PROBT(TO,K1)
- F2=PO
- TO=T3-E
- PO=PROBT(TO,K1)
- F2=F2-PO
- F2=F2/E2
- T4=T3-F1/F2
- IF ABS(T4-T3)>AO THEN DO
- T3=T4
- SIGNAL 'LABELA'
- END
- */
- T4=0
- DO FOREVER
- TO=T3
- PO=PROBT(TO,K1)
- F1=PO-P1
- TO=T3+E
- PO=PROBT(TO,K1)
- F2=PO
- TO=T3-E
- PO=PROBT(TO,K1)
- F2=F2-PO
- F2=F2/E2
- T4=T3-F1/F2
- IF ABS(T4-T3)>AO THEN T3=T4
- ELSE LEAVE
- END
- TO=T4
-
- RETURN TO
-
- LOGGAMMA: PROCEDURE
-
- ARG XA
- C1=76.18009173
- C2=-86.50532033
- C3=24.01409822
- C4=-1.231739516
- C5=.001208580
- C6=-.000005364
- C7=2.506628275
- X1=XA-1
- W=X1+5.5
- W=(X1+.5)*LN(W)-W
- S=1+C1/(X1+1)+C2/(X1+2)+C3/(X1+3)
- S=S+C4/(X1+4)+C5/(X1+5)+C6/(X1+6)
- L=W+LN(C7*S)
- RETURN L
-
- NORMALPP: PROCEDURE
-
- ARG P0
- A1=2.515517
- A2=.802853
- A3=.010328
- A4=1.432788
- A5=.189269
- A6=.001308
- Q=.5-ABS(P0-.5)
- SN=SIGN(-2*LN(Q))
- IF SN=-1 THEN W=-1*SQRT(ABS(-2*LN(Q))
- ELSE W=SQRT(-2*LN(Q))
- W1=A1+W*(A2+A3*W)
- W2=1+W*(A4+W*(A5+A6*W))
- ZZ=W-W1/W2
- SELECT
- WHEN (P0-.5)<0 THEN TT=-1
- WHEN (P0-.5)=0 THEN TT=0
- otherwise TT=1
- END
- ZZ=ZZ*TT
- RETURN ZZ
-
- FCRITICAL: PROCEDURE EXPOSE AC EC EC2
-
- PARSE ARG PO,K1,K2
- /* fIND NORMAL Z CORRESPONDING TO P */
- P1=PO
- Z=NORMALPP(PO)
- IF K2<3 THEN DO
- W=K2**.75
- W=Z/W
- Z=Z*(1.1581-W*(.2296+W*(.0042+.0027*W)))
- END
- /* FIND INITIAL APPROX. TO F */
- A1=2/(9*K1)
- A2=2/(9*K2)
- W=Z*Z
- W1=1+A2*(A2-W-2)
- W2=A1+A2-A1*A2-1
- W3=1+A1*(A1-W-2)
- SN=SIGN(W2*W2-W1*W3)
- IF SN=-1 THEN W3=-1*SQRT(ABS(W2*W2-W1*W3))
- ELSE W3=SQRT(W2*W2-W1*W3)
- FO=(W3-W2)/W1
- FO=FO**3
- /* MODIFIED NEWTON ITERATION TO IMPROVE VALUE OF F */
- DO FOREVER
- FCRIT=FO
- PO=PROBABILITY(FCRIT,K1,K2)
- F1=PO-P1
- FCRIT=FO+EC
- PO=PROBABILITY(FCRIT,K1,K2)
- F2=PO
- FCRIT=FO-EC
- PO=PROBABILITY(FCRIT,K1,K2)
- F2=F2-PO
- F2=F2/EC2
- F1=FO-F1/F2
- IF ABS(F1-FO)>AC THEN FO=F1
- ELSE LEAVE
- END
- FCRIT=F1
- RETURN FCRIT
-
-
-
- PROBABILITY: PROCEDURE EXPOSE AC EC EC2
-
- PARSE ARG F,K1,K2
- IF K1=1 THEN AC=.00001
- H2=K2/2
- H1=K1/2
- H3=H1+H2
- L1=0
- XX=H1
- LG=LOGGAMMA(XX)
- L1=L1+LG
- XX=H2
- LG=LOGGAMMA(XX)
- L1=L1+LG
- XX=H3
- LG=LOGGAMMA(XX)
- L1=L1-LG
- W1=K2/(K2+K1*F)
- XX=0
- IF H2<(H3*W1) THEN DO
- W2=W1
- W1=1-W1
- W3=H2
- H2=H1
- H1=W3
- END
- ELSE DO
- W2=1-W1
- XX=1
- END
- T1=1
- W3=1
- P=1
- I=INT(H1+W2*H3)
- W4=W1/W2
- /*LABELB:*/
- T2=H1-W3
- IF I=0 THEN W4=W1
- /*LABELC:*/
- DO FOREVER
- T1=T1*T2*W4/(H2+W3)
- P=P+T1
- T2=ABS(T1)
- IF (T2<=AC) & (T2<=AC*P) THEN LEAVE /*SIGNAL 'LABELD'*/
- W3=W3+1
- I=I-1
- IF I>=0 THEN DO /*SIGNAL 'LABELB'*/
- T2=H1-W3
- IF I=0 THEN W4=W1
- ITERATE
- END
- T2=H3
- H3=H3+1
- /*SIGNAL 'LABELC'*/
- END
- /*LABELD:*/
- W1=EXP(H2*LN(W1)+(H1-1)*LN(W2)-L1)
- P=P*W1/H2
- IF XX=0 THEN PO=P
- ELSE PO=1-P
- AC=.001
- RETURN PO
-
-
-